package com.gewu.pm.ui.adapter.order;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.SalesOrderBean;

/**
 * 销售订单适配器
 */
public final class SalesOrderAdapter extends AppAdapter<SalesOrderBean> {

    public SalesOrderAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvTerminalName;
        private final TextView tvStatus;
        private final TextView tvSubmitter;
        private final TextView tvSubmitTime;
        private final TextView tvOrderNo;
        private final TextView tvVehicleCode;
        private final TextView tvDistributor;
        private final TextView tvSalesman;
        private final TextView tvSalesArea;
        private final TextView tvAmount;

        private ViewHolder() {
            super(R.layout.sales_order_item);
            tvTerminalName = findViewById(R.id.tv_terminal_name);
            tvStatus = findViewById(R.id.tv_status);
            tvSubmitter = findViewById(R.id.tv_submitter);
            tvSubmitTime = findViewById(R.id.tv_submit_time);
            tvOrderNo = findViewById(R.id.tv_order_no);
            tvVehicleCode = findViewById(R.id.tv_vehicle_code);
            tvDistributor = findViewById(R.id.tv_distributor);
            tvSalesman = findViewById(R.id.tv_salesman);
            tvSalesArea = findViewById(R.id.tv_sales_area);
            tvAmount = findViewById(R.id.tv_amount);
        }

        @Override
        public void onBindView(int position) {
            SalesOrderBean item = getItem(position);

            tvTerminalName.setText(item.getTerminalName());
            tvSubmitter.setText(item.getSubmitter());
            tvSubmitTime.setText(item.getSubmitTime());
            tvOrderNo.setText(item.getOrderNo());
            tvVehicleCode.setText(item.getVehicleCode());
            tvDistributor.setText(item.getDistributor());
            tvSalesman.setText(item.getSalesman());
            tvSalesArea.setText(item.getSalesArea());
            tvAmount.setText(item.getAmount());

            // 设置状态标签
            tvStatus.setText(item.getStatusText());

            // 根据状态设置背景和颜色
            String status = item.getStatus();
            switch (status) {
                case "1": // 已审批
                    tvStatus.setBackgroundResource(R.drawable.shape_label_green);
                    tvStatus.setTextColor(getResources().getColor(R.color.green_01));
                    break;
                case "2": // 部分发货
                    tvStatus.setBackgroundResource(R.drawable.shape_label_orange);
                    tvStatus.setTextColor(getResources().getColor(R.color.orange_01));
                    break;
                case "3": // 未审批
                case "4": // 已发货
                case "5": // 已完成
                case "6": // 已取消
                default:
                    tvStatus.setBackgroundResource(R.drawable.shape_label_gray);
                    tvStatus.setTextColor(getResources().getColor(R.color.cb9));
                    break;
            }
        }
    }
}

